<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

use think\Db;


class Login extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $data = request()->put();

        if (empty($data['name'])) {
            return json(array('code' => 400, 'message' => '请输入账号'));
        }
        $name = $data['name'];
        if (empty($data['password'])) {
            return json(array('code' => 400, 'message' => '请输入密码'));
        }




        
        

        $password = $data['password'];
        $map['password'] = md5($password);
        $map['account'] = $name;
        $map['status'] = 1;

        $result = Db::table("zstp_user")->where($map)->find();

    


        if(empty($result)){
           
            return json(array('code' => 400, 'message' => '账号或密码错误'));
        }

     


        
        $name = $result['name'];
        $id = $result['id'];
        $token = getToken($name, $id);
        return json(array('code' => 200, 'message' => "登录成功", "data" => array("token" => $token)));
    }

    public function info(Request $request)
    {

        $token = $request->get("token");
        $tokenData = verifyToken($token);
        $result = Db::table("zstp_user")->where(array("id"=>$tokenData['id']))->find();
        if (empty($tokenData)) {
            $data = array(
                "code" => 400,
                "message" => "登录超时",
                "data" => $tokenData,
            );
            return json($data);
        }
        $tokenData['roles'] = array(  $result['roles']);
        $data = array(
            "code" => 200,
            "message" => "success",
            "data" => $tokenData,
        );
        return json($data);
    }

    public function logout()
    {
        $data = array(
            "code" => 200,
            "message" => "success",
            "data" => array()
        );
        return json($data);
    }
}
